package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.databinding.LayoutDialogEditGoodsBinding;
import com.cloudcreativity.blyg.sqlite.CategoryOne;
import com.cloudcreativity.blyg.sqlite.CategoryTwo;
import com.cloudcreativity.blyg.sqlite.Goods;

import java.math.BigDecimal;

public class EditGoodsUtils extends Dialog {

    public ObservableField<String> name = new ObservableField<>();
    public ObservableField<String> code = new ObservableField<>();
    public ObservableField<String> brand = new ObservableField<>();
    public ObservableField<String> space = new ObservableField<>();
    public ObservableField<String> price = new ObservableField<>();
    public ObservableField<String> unit = new ObservableField<>();
    public ObservableField<String> stock = new ObservableField<>();
    public ObservableField<String> category = new ObservableField<>();
    private OnOkListener onOkListener;

    private Goods entity;

    public void setOnOkListener(OnOkListener onOkListener) {
        this.onOkListener = onOkListener;
    }

    public EditGoodsUtils(@NonNull Context context,Goods goods) {
        super(context, R.style.myProgressDialogStyle);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        this.entity = goods;
        this.name.set(goods.getName());
        this.category.set(this.entity.getCategoryOne().getName()+"/"+this.entity.getCategoryTwo().getName());
        this.code.set(goods.getBarCode());
        this.brand.set(goods.getBrandName());
        this.space.set(goods.getSpecName());
        this.price.set(String.valueOf(StrUtils.get2BitDecimal(goods.getPrice()/100f)));
        this.unit.set(goods.getUnit());
        this.stock.set(String.valueOf(StrUtils.get2BitDecimal(goods.getStock())));
        LayoutDialogEditGoodsBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.layout_dialog_edit_goods,
                null,false);
        binding.setUtils(this);
        setContentView(binding.getRoot());
    }

    public void onCategoryClick(){
        CategoryDialog dialog = new CategoryDialog(getContext());
        dialog.setOnOkListener(new CategoryDialog.OnOkListener() {
            @Override
            public void onOk(CategoryOne one, CategoryTwo two) {
                entity.setCategoryOne(one);
                entity.setCategoryTwo(two);
                entity.setCategoryOneId(one.getId());
                entity.setCategoryTwoId(two.getId());
            }
        });
        dialog.show();
    }

    public void onOk(){
        if(TextUtils.isEmpty(name.get())){
            ToastUtils.showShortToast(getContext(),"名称不能为空");
            return;
        }
//        if(TextUtils.isEmpty(brand.get())){
//            ToastUtils.showShortToast(getContext(),"品牌不能为空");
//            return;
//        }
//        if(TextUtils.isEmpty(space.get())){
//            ToastUtils.showShortToast(getContext(),"规格不能为空");
//            return;
//        }
        if(TextUtils.isEmpty(price.get())){
            ToastUtils.showShortToast(getContext(),"价格不能为空");
            return;
        }
//        if(TextUtils.isEmpty(name.get())){
//            ToastUtils.showShortToast(getContext(),"单位不能为空");
//            return;
//        }

        Goods goods = new Goods();
        int sellPrice = new BigDecimal(price.get()).multiply(new BigDecimal(100)).intValue();
        double stockNum = TextUtils.isEmpty(stock.get())?0:Double.parseDouble(stock.get());

        goods.setId(entity.getId());
        goods.setLid(entity.getLid());
        goods.setName(name.get());
        goods.setPrice(sellPrice);
        goods.setBarCode(code.get());
        goods.setBrandName(brand.get());
        goods.setCategoryOneId(entity.getCategoryOneId());
        goods.setCategoryTwoId(entity.getCategoryTwoId());
        goods.setUnit(unit.get());
        goods.setSpecName(space.get());
        goods.setStock(stockNum);
        if(onOkListener!=null){
            onOkListener.onOk(goods);
        }
        KeyBoardHelper.close(getContext(),this);
        dismiss();
    }

    public interface OnOkListener{
        void onOk(Goods goods);
    }

    public void onClose(){

        KeyBoardHelper.close(getContext(),this);
        dismiss();
    }
}
